package com.example.index.widget

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.index.adapter.FooViewBinder
import com.example.index.adapter.foo.Foo
import me.drakeet.multitype.MultiTypeAdapter

class FooRV : RecyclerView {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        init()
    }


    fun init() {
        adapter = createFooAdapter()
        if (layoutManager == null) {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
    }

    private fun createFooAdapter(): Adapter<*>? {
        val rvAdapter = MultiTypeAdapter()
        rvAdapter.items = createFooData()
        rvAdapter.register(Foo::class.java,FooViewBinder())

        return rvAdapter
    }

    private fun createFooData(): List<Foo> {

        val res = arrayListOf<Foo>()
        for (i in 0..32) {

            res.add(Foo("item $i"))
        }
        return res
    }

}